package com.chinanet.netelementmodel;

import com.chinanet.entry.netelement.Attr;
import com.chinanet.entry.netelement.AttrType;
import com.chinanet.entry.netelement.WorkInterFace;
import com.chinanet.utils.webservice.AttrsIn;
import com.chinanet.utils.webservice.InintMap;
import com.chinanet.utils.webservice.WebService;

import java.util.HashMap;
import java.util.Map;

/**
 * H_WebService_IPTV_Imp_getHwIptvMac
 * 华为IPTV接口-查询MAC地址
 * @author xuzhi
 *
 */
public class H_WebService_IPTV_Imp_getHwIptvMac	implements WorkInterFace {

	@Override
	public String getName() {
		return "华为IPTV接口-查询MAC地址";
	}

	@Override
	public String getDesciption() {
		return null;
	}

	@Override
	public int getVersion() {
		return 0;
	}

	@Override
	public Attr[] getInAttrs() {
		Attr[] in=new Attr[1];
		in[0]=new Attr("UserID", "IPTV用户帐号，参数只包含数字、字母、下划线、点和 '@'符号", AttrType.TYPE_String, true);
//		in[1]=new Attr("SequenceID", "操作流水号，唯一识别.可使用yyymmddhh24miss+3位数字序列号的生成规则", AttrType.TYPE_String, true);
		return in;
	}

	@Override
	public Attr[] getOutAttrs() {
		Attr[] out = new Attr[8];
		out[0]=new Attr("ReturnCode", "0：成功；其它：失败", AttrType.TYPE_Int, true);
		out[1]=new Attr("ReturnMessage", "失败时返回错误信息", AttrType.TYPE_String, true);
		out[2]=new Attr("Mac", "用户mac信息", AttrType.TYPE_String, true);
		out[3]=new Attr("log", "服务器返回的soapxml", AttrType.TYPE_String, true);
		out[4]=new Attr("Errormsg", "服务器返回的错误信息,当ReturnCode为0的时候就不必去读取", AttrType.TYPE_String, false);
		out[5]=new Attr("deviceManufactor", "机顶盒厂家", AttrType.TYPE_String, true);
        out[6]=new Attr("deviceType", "机顶盒型号", AttrType.TYPE_String, true);
        out[7]=new Attr("platform", "适用平台", AttrType.TYPE_String, true);
		return out;
	}


	@Override
	public Map<String, Object> init(Map<String, Object> data) {
		AttrsIn attrsIn=new AttrsIn();
		WebService webService=new WebService();
		Map<String,Object> map=new HashMap<String, Object>();
		//Get into arguments
		attrsIn.setClientSysName("10000号");
		attrsIn.setFunctionName("getHwIptvMac");
		attrsIn.setRequestMessage("<UserID>"+(String) data.get("UserID")+"</UserID><SequenceID>"+webService.getrandom()+"</SequenceID>");
		//Get soapxml
		String returnXml=webService.WebServiceInit(attrsIn);
		String flag=webService.getPattern("flag", returnXml);
		if(flag.equals("true")){
			map.put("ReturnCode", Integer.parseInt(webService.getPattern("ReturnCode", returnXml)));
			map.put("ReturnMessage", webService.getPattern("ReturnMessage", returnXml));
			map.put("Mac", webService.getPattern("Mac", returnXml));

			String mac=webService.getPattern("Mac", returnXml).trim();
			map.put("log", webService.dealkouhao(returnXml));
			map.put("Errormsg", "");
			map.put("flag",1);

			if(mac!=null && mac.length()>8){
			    String macPrefix=mac.substring(0,8);
			    if (InintMap.map.isEmpty()){
			        InintMap.getmap();
                }
                String macAddress=InintMap.map.get(macPrefix);
			    if(macAddress!=null &&macAddress.length()>0) {
					String[] strs = macAddress.split("&&");
					if (strs.length > 2) {
						map.put("deviceManufactor", strs[0]);
						map.put("deviceType", strs[1]);
						map.put("platform", strs[2]);
					} else {
						map.put("deviceManufactor", "未知厂家");
						map.put("deviceType", "未知型号");
						map.put("platform", "未知适用平台");
					}
				}else{
					map.put("deviceManufactor", "未知厂家");
					map.put("deviceType", "未知型号");
					map.put("platform", "未知适用平台");
				}
            }else{
                map.put("deviceManufactor","未知厂家");
                map.put("deviceType","未知型号");
                map.put("platform","未知适用平台");
            }
		}else{
			map.put("flag",0);
			map.put("ReturnCode", 1);
			map.put("ReturnMessage","");
			map.put("Mac", "");
            map.put("deviceManufactor","未知厂家");
            map.put("deviceType","未知型号");
            map.put("platform","未知适用平台");
			map.put("log", webService.dealkouhao(returnXml));
			map.put("Errormsg", "网元侧执行出错，处理请求失败");
		}

		return map;
	}



}
